﻿/**
 * @file util.hpp
 * @date 2021/04/25 14:11
 *
 * @author harrybao (harrybao@123u.com)
 *
 * @brief 工具
 *
 * @version v1.0.0
 *
 * @copyright Copyright © 2021 All Rights Reserved 上海欢乐互娱网络科技有限公司
 *
 * @par history:
 * <table>
 * <tr><th>Date                     <th>Version     <th>Author <th>Description
 * <tr><td>2021/04/25 14:11         <td>1.0.0       <td>harrybao
 * <td>创建初始版本
 * </table>
 */

// #ifndef UTIL_HPP
// #define UTIL_HPP

#pragma once

#include <string>
#include <vector>
#include "box/service_context.hh"
#include "service_tool.hpp"
#include "singleton.hpp"
#include "util/util.hh"


namespace guild_service {
namespace common {

/**
 * @brief 服务工具类
 *
 */
class util : public service_tool {
 public:
  using service_context_type = kratos::service::ServiceContext;
  using util_ptr_type = kratos::service::UtilPtr;

  util() : service_tool(), m_util_ptr(nullptr) {}

  util(service_context_type* context)
      : service_tool(), m_util_ptr(std::move(context->new_util())) {}

  virtual ~util() = default;

  const util_ptr_type& operator->() const { return m_util_ptr; }
  
  // split string by 'delim' and save sub-string to 'result', the 'delim' will
  // keep in result
  // @param src source string
  // @param delim delimeter
  // @param result sub-string result
  void splitBy(const std::string &src, char delim,
               std::vector<std::string> &result) {
    result.clear();
    if (src.empty()) {
      return;
    }
    std::string item;
    for (auto &c : src) {
      if (c != delim) {
        item.push_back(c);
      } else {
        item.push_back(delim);
        result.push_back(std::move(item));
      }
    }
    if (!item.empty()) {
      result.push_back(std::move(item));
    }
  }

 private:
  /// 服务工具指针
  util_ptr_type m_util_ptr;
};

}  // namespace common

#define UtilSingleton(...)                                                   \
  (*guild_service::common::singleton<guild_service::common::util>::instance( \
      __VA_ARGS__))

}  // namespace guild_service

// #endif // UTIL_HPP
